﻿// -----------------------------------------------------------------------
// <copyright file="EmployeeManager.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace employee_test
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    

    /// <summary>
    /// Manager for Employee table
    /// </summary>
    public class EmployeeManager : EntityManager
    {
        /// <summary>
        /// Gets the employees.
        /// </summary>
        /// <param name="departmentID">The department ID.</param>
        /// <param name="jobLevelID">The JobLevel ID.</param>
        /// <returns>List of employees with the specified department ID and Job Level ID</returns>
        public static List<Employee> GetEmployees(int? departmentID, int? jobLevelID)
        {
            string query = "SELECT * FROM Employee WHERE 1=1";

            if (departmentID.HasValue)
            {
                query += " and DepartmentID=" + departmentID;
            }

            if (jobLevelID.HasValue)
            {
                query += " and JobLevelID=" + jobLevelID;
            }

            try
            {
                return Context.ExecuteQuery<Employee>(query, new object[] { }).ToList<Employee>();
            }
            catch (Exception ex)
            {
                // Log Exception
                Logger.LogException(ex);
                return null;
            }
        }

        /// <summary>
        /// Gets the employees.
        /// </summary>
        /// <returns>List of all employees</returns>
        public static List<Employee> GetEmployees()
        {
            List<Employee> employees = Context.Employees.ToList<Employee>();
            return employees;
        }

        /// <summary>
        /// Gets the employee by ID.
        /// </summary>
        /// <param name="employeeID">The employee ID.</param>
        /// <returns>The employee.</returns>
        public static Employee GetEmployeeByID(int employeeID)
        {
            Employee[] employees = Context.Employees.Where(emp => emp.EmployeeID == employeeID).ToArray();
            if (employees.Length != 1)
            {
                throw new Exception("Invalid employee ID");
            }
            else
            {
                return employees[0];
            }
        }

        /// <summary>
        /// Adds the employee.
        /// </summary>
        /// <param name="employeeName">Name of the employee.</param>
        /// <param name="departmentID">The department ID.</param>
        /// <param name="jobLevelID">The job level ID.</param>
        /// <param name="title">The title.</param>
        /// <param name="salary">The salary.</param>
        public static void AddEmployee(string employeeName, int departmentID, string title, double salary)
        {
            Employee employee = new Employee();
            employee.EmployeeName = employeeName;
            employee.DepartmentID = departmentID;
            employee.Title = title;
            employee.Salary = salary;
            try
            {
                Context.Employees.InsertOnSubmit(employee);
                Context.SubmitChanges();
            }
            catch (Exception ex)
            {
                // Log Exception
                Logger.LogException(ex);
                throw ex;
            }
        }
    }
}

